package es.task;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.ycl.deupload.controller.EasyExcel;
import com.ycl.deupload.entity.BaseUser;
import com.zbf.common.entity.my.BaseExportTaskDetail;
import com.zbf.common.entity.my.BaseExportTaskHeader;
import com.zbf.common.entity.my.Importexcel;
import es.dao.UserDataMapper;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.checkerframework.checker.units.qual.A;

/**
 * 作者: LCG
 * 日期: 2020/7/28 09:06
 * 描述:
 */
@Component
public class MyTask01 implements SimpleJob {

    @Value("${server.port}")
    private String port;

    @Autowired
   private UserDataMapper userDataMapper;



    @Override
    public void execute(ShardingContext shardingContext) {
     //   System.out.println("==========将来大家写自己的业务==========");

        //获取分片的总数
        int shardingTotalCount = shardingContext.getShardingTotalCount();
       // System.out.println(shardingTotalCount);
        //获取分片项
        int shardingItem = shardingContext.getShardingItem();
      //  System.out.println(shardingItem);

        EasyExcel easyExcel = new EasyExcel();

      //  int getheaderid = userDataMapper.getheaderid();
       // System.out.println(getheaderid);
       // if (getheaderid > 0) {
            //BaseExportTaskHeader getid1 = userDataMapper.getid(getheaderid);
        List<BaseExportTaskHeader> getlistheader = userDataMapper.getlistheader();
        for (BaseExportTaskHeader taskHeader:getlistheader) {
            Long getheaderid = taskHeader.getId();
            if (taskHeader.getTaskStatus() == 1) {
                for (int i = 0; i < taskHeader.getTaskTailNum(); i++) {
                    if (i == shardingItem) {
                        //  System.out.println(port + "==============0-3000" + "--->" + shardingContext.getShardingParameter());
                        Integer version = taskHeader.getVersion();
                        long start = System.currentTimeMillis();
                        try {
                            BaseExportTaskDetail getidheid = userDataMapper.getidheid(getheaderid, i);
                            String taskSql = getidheid.getTaskSql();
                            System.out.println("++++++" + taskSql);
                            List<Importexcel> sqlLimit = userDataMapper.getSqlLimit(taskSql);
                            //System.out.println(sqlLimit);
                            easyExcel.exporteasyExcel(sqlLimit, getidheid.getFileName());


                            Integer version1 = taskHeader.getVersion();
                            if (version == version1) {
                                userDataMapper.updateHeader(getheaderid);
                            } else {
                                userDataMapper.updateHeader(getheaderid);
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        long end = System.currentTimeMillis();
                        System.out.println("分片0==========" + (end - start) / 1000.0);

                        break;
                    } else if (i == shardingItem) {
                        //   System.out.println(port + "==============3001-6000");
                        Integer version = taskHeader.getVersion();
                        long start = System.currentTimeMillis();
                        try {
                            BaseExportTaskDetail getidheid = userDataMapper.getidheid(getheaderid, i);
                            String taskSql = getidheid.getTaskSql();
                            System.out.println("++++++" + taskSql);
                            List<Importexcel> sqlLimit = userDataMapper.getSqlLimit(taskSql);
                            //System.out.println(sqlLimit);
                            easyExcel.exporteasyExcel(sqlLimit, getidheid.getFileName());


                            Integer version1 = taskHeader.getVersion();
                            if (version == version1) {
                                userDataMapper.updateHeader(getheaderid);
                            } else {
                                userDataMapper.updateHeader(getheaderid);
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        long end = System.currentTimeMillis();
                        System.out.println("分片1==========" + (end - start) / 1000.0);

                        break;
                    } else if (i == shardingItem) {
                        //   System.out.println(port + "==============6001-10000");
                        Integer version = taskHeader.getVersion();
                        long start = System.currentTimeMillis();
                        try {
                            BaseExportTaskDetail getidheid = userDataMapper.getidheid(getheaderid, i);
                            String taskSql = getidheid.getTaskSql();
                            System.out.println("++++++" + taskSql);
                            List<Importexcel> sqlLimit = userDataMapper.getSqlLimit(taskSql);
                            //System.out.println(sqlLimit);
                            easyExcel.exporteasyExcel(sqlLimit, getidheid.getFileName());


                            Integer version1 = taskHeader.getVersion();
                            if (version == version1) {
                                userDataMapper.updateHeader(getheaderid);
                            } else {
                                userDataMapper.updateHeader(getheaderid);
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        long end = System.currentTimeMillis();
                        System.out.println("分片2==========" + (end - start) / 1000.0);

                        break;
                    } else if (i == shardingItem) {
                        //   System.out.println(port + "==============6001-10000");
                        Integer version = taskHeader.getVersion();
                        long start = System.currentTimeMillis();
                        try {
                            BaseExportTaskDetail getidheid = userDataMapper.getidheid(getheaderid, i);
                            String taskSql = getidheid.getTaskSql();
                            System.out.println("++++++" + taskSql);
                            List<Importexcel> sqlLimit = userDataMapper.getSqlLimit(taskSql);
                            //System.out.println(sqlLimit);
                            easyExcel.exporteasyExcel(sqlLimit, getidheid.getFileName());


                            Integer version1 = taskHeader.getVersion();
                            if (version == version1) {
                                userDataMapper.updateHeader(getheaderid);
                            } else {
                                userDataMapper.updateHeader(getheaderid);
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        long end = System.currentTimeMillis();
                        System.out.println("=分片3=========" + (end - start) / 1000.0);
                        break;
                    }
                }

                BaseExportTaskHeader getid = userDataMapper.getid(getheaderid);
                if(getid.getVersion()==4 && getid.getTaskTailOkNum()==4){
                    int i = userDataMapper.updateHeaderstatus(getid.getId());
                }
            } else {
                System.out.println("请先去前台执行");
            }
        }

       // }

    }
}
